package service

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"litter-api/app/util"
	"litter-api/app/util/pool"
	"net/http"
	"time"
)

var ()

// 构建一个Post请求
func BuildPostApiReq(url string, param interface{}, headers map[string]interface{}, timeOut int) (map[string]interface{}, error) {

	// 设置请求Url-请自行设置Url
	pool.Logger.Info("请求接口: " + url)
	// json转换
	JsonData, _ := json.Marshal(&param)
	// 记录参数
	pool.Logger.Info("参数: " + string(JsonData))
	// 设置header
	reader := bytes.NewReader(JsonData)
	// Post请求
	req, err := http.NewRequest("POST", url, reader)
	// Json格式数据
	if err != nil {
		pool.Logger.Info("请求未成功发送: " + err.Error())
		return make(map[string]interface{}), err
	}

	// 设置请求的header信息
	req.Header.Add("Content-Type", "application/json")
	JsonHeaderData, _ := json.Marshal(&headers)
	pool.Logger.Info("header参数: " + string(JsonHeaderData))

	for key, header := range headers {
		req.Header.Add(key, fmt.Sprintf("%v", header))
	}
	client := &http.Client{Timeout: time.Duration(timeOut) * time.Millisecond}
	// 发送请求
	resp, err := client.Do(req)

	if err != nil {
		pool.Logger.Info("请求出现错误: " + err.Error())
		return make(map[string]interface{}), err
	}
	defer resp.Body.Close()
	// 读取数据
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		pool.Logger.Info("请求出现错误: " + err.Error())
		return make(map[string]interface{}), err
	}
	pool.Logger.Info("返回状态码为: " + resp.Status)
	respMap := util.ToJsonMap(string(body))
	pool.Logger.Info("响应: " + string(body))

	return respMap, nil

}

// 构建一个Post请求
/*func BuildCrmPostApiReq(url string, param interface{}, headers map[string]interface{}, timeOut int) (map[string]interface{}, error) {

	// 设置请求Url-请自行设置Url
	pool.Logger.Info("请求接口: " + url)
	// json转换
	JsonData, _ := json.Marshal(&param)
	// 记录参数
	pool.Logger.Info("参数: " + string(JsonData))
	// 设置header
	reader := bytes.NewReader(JsonData)
	// Post请求
	req, err := http.NewRequest("POST", url, reader)
	// Json格式数据
	if err != nil {
		pool.Logger.Info("响应时间: " + util.ToString(time.Now().Format("2006-1-2 15:04:05.000")))
		pool.Logger.Info("请求CRM未成功发送: " + err.Error())
		return make(map[string]interface{}), err
	}

	// 设置请求的header信息
	req.Header.Add("Content-Type", "application/json")
	JsonHeaderData, _ := json.Marshal(&headers)
	pool.Logger.Info("header参数: " + string(JsonHeaderData))

	for key, header := range headers {
		req.Header.Add(key, fmt.Sprintf("%v", header))
	}
	startMilliSed := time.Now().UnixMilli()
	pool.Logger.Info("请求时间: " + util.ToString(time.Now().Format("2006-1-2 15:04:05.000")))
	//client := &http.Client{Timeout: time.Duration(timeOut) * time.Millisecond}
	client := &http.Client{
		// 协议层面
		Transport: &http.Transport{
			// 连接超时
			DialContext: (&net.Dialer{
				Timeout: 200 * time.Millisecond, // 连接超时
			}).DialContext,
			//HTTPS请求的TLS连接
			DialTLSContext: (&net.Dialer{
				Timeout: 200 * time.Millisecond, // 连接超时
			}).DialContext,
			DisableKeepAlives: true,
			ForceAttemptHTTP2: true,
			// tls握手时间
			TLSHandshakeTimeout: 200 * time.Millisecond,
		},
		// 超时时间
		Timeout: time.Duration(timeOut) * time.Millisecond,
	}
	// 发送请求
	resp, err := client.Do(req)
	endMilliSed := time.Now().UnixMilli()
	pool.Logger.Info("请求CRM耗时: " + util.ToString(endMilliSed-startMilliSed) + "ms")

	if err != nil {
		pool.Logger.Info("响应时间: " + util.ToString(time.Now().Format("2006-1-2 15:04:05.000")))
		pool.Logger.Info("CRM请求出现错误: " + err.Error())
		return make(map[string]interface{}), err
	}
	defer resp.Body.Close()
	// 读取数据
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		pool.Logger.Info("响应时间: " + util.ToString(time.Now().Format("2006-1-2 15:04:05.000")))
		pool.Logger.Info("CRM请求出现错误: " + err.Error())
		return make(map[string]interface{}), err
	}
	pool.Logger.Info("返回状态码为: " + resp.Status)
	respMap := util.ToJsonMap(string(body))
	pool.Logger.Info("响应: " + string(body))
	pool.Logger.Info("响应时间: " + util.ToString(time.Now().Format("2006-1-2 15:04:05.000")))
	return respMap, nil

}*/
